<html>
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<head>
<title>Section A.15.&nbsp; Chapter 15</title>
<link rel="STYLESHEET" type="text/css" href="images/style.css">
<link rel="STYLESHEET" type="text/css" href="images/docsafari.css">
<script type="text/javascript"><!--
google_ad_client = "pub-0203281046321155";
google_alternate_ad_url = "http://www.bookhtml.com/adbrite.htm";
google_ad_width = 728;
google_ad_height = 90;
google_ad_format = "728x90_as";
google_ad_type = "text_image";
google_ad_channel ="4867465545";
google_color_border = "FFFFFF";
google_color_link = "0000FF";
google_color_bg = "FFFFFF";
google_color_text = "000000";
google_color_url = "0000FF";
//--></script>
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-APP-A-SECT-14.html><img src="images/prev.gif" width="60" height="17" border="0" align="absmiddle" alt="Previous Page"></a>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-APP-A-SECT-16.html><img src="images/next.gif" width="60" height="17" border="0" align="absmiddle" alt="Next Page"></a>
</div></td></tr></table>
<br><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><TD valign="top"><a name="learnphpmysql-APP-A-SECT-15"></a>
<h3 id="631450-812" class="docSection1Title">A.15. Chapter 15</h3>
<dl class="docList">
<dt><br><p><span class="docPubcolor"><span class="docEmphStrong">Solution to Question 15-1</span></span></p></dt>
<dd><p class="docList">JavaScript's pros are users get immediate feedback when entering data into fields about that data's validity, and the form doesn't need to be redisplayed by the PHP code.</p>
<p class="docList">One of JavaScript's cons is that the data must still be validated in your PHP code because it's possible for a user to turn off JavaScript in her browser or for a malicious user to directly submit data to your form-processing script. Additionally, the validation doesn't have access to any of the server datafor example, session information or database information.</p>
</dd>

<dt><br><p><span class="docPubcolor"><span class="docEmphStrong">Solution to Question 15-2</span></span></p></dt>
<dd><p class="docList">To display the warning "The username field must be at least six characters," execute:</p>
<pre>
alert("The username field must be at least six characters");
</pre><br>

</dd>

<dt><BR><p><span class="docPubcolor"><span class="docEmphStrong">Solution to Question 15-3</span></span></p></dt>
<dd><p class="docList">Validate a U.S. zip code that may have the optional "plus four" style as follows:</P>
<pre>
'/^\d{5}(-\d{4})?$/'
</pre><BR>

<p class="docList">Remember that the regex expression must be in Perl format, which starts with <tt>'/</tt> and ends with <tt>/'</tt>.</p>
</dd>

<dt><BR><p><span class="docPubcolor"><span class="docEmphStrong">Solution to Question 15-4</span></span></p></dt>
<dd><p class="docList">To test a variable called <tt>$zipcode</tt> using the regex from the last question, specify:</p>
<pre>
&lt;?php
$pattern = '/^\d{5}(-\d{4})?$/';
$matched=preg_match($pattern, $zipcode, $matches);
if ($matched) {
  echo ("Zipcode OK.");
}
?&gt;
</pre><BR>

</dd>
</dl>

</TD></TR></table>
<br>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-APP-A-SECT-14.html><img src="images/prev.gif" width="60" height="17" border="0" align="absmiddle" alt="Previous Page"></a>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-APP-A-SECT-16.html><img src="images/next.gif" width="60" height="17" border="0" align="absmiddle" alt="Next Page"></a>
</div></td></tr></table>
</body></html>
